package znbkTestPaperBj.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.example.lancooznbk.view.widget.EmptyMainLayout;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;
import com.lancoo.reviewsystem.utils.ToastUtils;

import lancooControl.AndroidBug5497Workaround;
import znbkListBj.entity.NoteListEntity;
import znbkListSc.entity.CollectionListEntity;
import znbkTestPaperBj.ZnbkPaperBjInfor;
import znbkTestPaperBj.entity.NotePaperEntity;
import znbkTestPaperBj.fragmentAdapter.NoteCheckOriginalQuestionPaperFragmentAdapter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperInfor.ZnbkPaperInfor;
import znbkTestPaperSc.ZnbkPaperScInfor;
import znbkTestPaperSc.activity.CollectionPaperActivity;
import znbkTestPaperSc.presenter.CollectionPaperPresenter;

import static znbkTestPaperBj.activity.NotePaperActivity.mEntityBj;

/**
 * Created by Administrator on 2018\3\23 0023.
 */

public class NoteCheckOriginalQuestionPaperActivity extends BaseActivity implements View.OnLayoutChangeListener{

    private ScaleButton mSBtnBack;//返回按钮
    private TextView mTvTittle;
    private ScaleButton mSBtnMore;
    public static ViewPager mVp;
    private NoteCheckOriginalQuestionPaperFragmentAdapter mAdapter;

    private NoteListEntity.NoteBean mEntity;
    public static final  String PAPER_ENTITY = "paperEntity";
    public static final String PAPER_FROM = "paperFrom";

    public int mQuesOrPaperFrom;
    private LoadingDialog mLoadingDialog;

    //Activity最外层的Layout视图
    private View activityRootView;


    public  static void launch(BaseActivity activity,@ZnbkPaperDef.PaperFrom int quesOrPaperFrom){

//        if(mEntityBj.getModelTKQue().getResType().equals("1")||
//                mEntityBj.getModelTKQue().getResType().equals("2")||
//                mEntityBj.getModelTKQue().getResType().equals("3")||
//                mEntityBj.getModelTKQue().getResType().equals("4")||
//                mEntityBj.getModelTKQue().getResType().equals("5")||
//                mEntityBj.getModelTKQue().getResType().equals("6")) {
//            ZnbkPaperScInfor.Flag = 1;
//        }else{
//            ZnbkPaperScInfor.Flag = 0;
//        }
//
//        if(mEntityBj.getModelTKQue().getResType().equals("Q")) {
//            ZnbkPaperInfor.ResFlag = 0;
//        }else{
//            ZnbkPaperInfor.ResFlag = 1;
//        }

        Bundle bundle = new Bundle();
        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
        Intent intent = new Intent(activity, NoteCheckOriginalQuestionPaperActivity.class);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent,1);

//        Bundle bundle = new Bundle();
//        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
//        Intent intent = new Intent(activity, NoteCheckOriginalQuestionPaperActivity.class);
//        intent.putExtras(bundle);
//        activity.startActivityForResult(intent,1);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.znbk_activity_paper_easy;
    }

    @Override
    protected void initViews() {

        AndroidBug5497Workaround.assistActivity(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            mEntity = (NoteListEntity.NoteBean) bundle.getSerializable(PAPER_ENTITY);
            mQuesOrPaperFrom = bundle.getInt(PAPER_FROM);
        }
        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }


        mEmptyViewLayout = (EmptyMainLayout) findViewById(R.id.layout_loading);
        mEmptyViewLayout.hide();
        /*
        * 返回按钮菜单相关设置
        * */
        mSBtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HideKeyboard();
                finish();
            }
        });


        /*
        * 页面头部-》收藏详情：mTvTittle
        * */
        mTvTittle = (TextView) findViewById(R.id.tv_title);
        mTvTittle.setText(R.string.znbk_titleNoteOriginalQuestion);

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSBtnMore = (ScaleButton) findViewById(R.id.sbtn_more);
        mSBtnMore.setVisibility(View.GONE);

        mVp = (ViewPager) findViewById(R.id.viewpager);

        activityRootView = findViewById(R.id.main);

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mSBtnMore.setVisibility(View.GONE);

        //这里因为是3.0一下版本，所以需继承FragmentActivity，通过getSupportFragmentManager()获取FragmentManager
        //3.0及其以上版本，只需继承Activity，通过getFragmentManager获取事物
        FragmentManager fm = getSupportFragmentManager();
        //初始化自定义适配器MyFragmentPageAdapter
        mAdapter = new NoteCheckOriginalQuestionPaperFragmentAdapter(fm);
        MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();
        mVp.setOnPageChangeListener(myOnPageChangeListener);
        //绑定自定义适配器
//        if(data.getAnswerState() == 2){
//            mAdapter.paperHasSubmit();
//        }
        mVp.setAdapter(mAdapter);

    }

//    @Override
//    public void loadPaperData(NotePaperEntity data) {
//
//        //这里因为是3.0一下版本，所以需继承FragmentActivity，通过getSupportFragmentManager()获取FragmentManager
//        //3.0及其以上版本，只需继承Activity，通过getFragmentManager获取事物
//        FragmentManager fm = getSupportFragmentManager();
//        //初始化自定义适配器MyFragmentPageAdapter
//        mAdapter =  new NoteCheckOriginalQuestionPaperFragmentAdapter(fm);
//        MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();
//        mVp.setOnPageChangeListener(myOnPageChangeListener);
//        //绑定自定义适配器
////        if(data.getAnswerState() == 2){
////            mAdapter.paperHasSubmit();
////        }
//        mVp.setAdapter(mAdapter);
//
//    }
//
//    @Override
//    public void loadComplete(boolean hasComplete) {
//
//    }
//
//    @Override
//    public void openExamReportDialog() {
//
//    }
//
//    @Override
//    public void updatingResult(boolean isSucceed) {
//        mLoadingDialog.dismiss();
//        if (isSucceed){
//        }else {
//            ToastUtils.showShort("提交失败");
//        }
//    }

    public  LoadingDialog getLoadingDialog(){
        return mLoadingDialog;
    }

    /***********************************************************************************************************************
     /*
     * LazyViewPage切换事件监听
     * */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageSelected(int arg0) {

        }

    }


    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = this.getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
    * 监听系统home事件
    * */
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

    /*
    * 监听系统返回事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            return false;
        }
        return false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        //添加layout大小发生改变监听器
        activityRootView.addOnLayoutChangeListener(this);
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        if (bottom > oldBottom){
            //输入法软键盘关闭
            Log.d(">>>>>>>>>crg", "输入法软键盘关闭: ");
        } else if (bottom < oldBottom){
            //输入法软键盘打开
            Log.d(">>>>>>>>>crg", "输入法软键盘打开: ");
        }

    }

}
